package com.hjli.tool;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.math.BigInteger;
import java.nio.MappedByteBuffer;
import java.nio.channels.FileChannel;
import java.security.MessageDigest;
import java.util.ArrayList;
import java.util.List;

import org.apache.commons.codec.digest.DigestUtils;
import org.apache.commons.fileupload.disk.DiskFileItem;
import org.apache.commons.io.IOUtils;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.commons.CommonsMultipartFile;

public class FileTool {

	/**
	 * @param File
	 *            file
	 * @return List<String>
	 */
	public static List<String> readFile(File file) {
		List<String> list = new ArrayList<String>();
		File[] fs = file.listFiles();
		for (File f : fs) {
			 System.out.println(f.isDirectory() ? "文件夹：" + f.getAbsolutePath()
			 : "文件：" + f.getName());
			list.add(f.getName());
			if (f.isDirectory()) {
				readFile(f);
			}
		}
		return list;
	}
	public List<String> getFloderName(File file){
		List<String> list = new ArrayList<String>();
		File[] fs = file.listFiles();
		for (File f : fs) {
//			 System.out.println(f.isDirectory() ? "文件夹：" + f.getAbsolutePath()
//			 : "文件：" + f.getName());
			if (f.isDirectory()) {
				list.add(f.getAbsolutePath());
				getFloderName(f);
			}
		}
		return list;
	}

	/**
	 * 获取txt 文件值
	 * 
	 * @param File
	 *            file
	 * @return String
	 */
	public static String readTXT(File file) {
		StringBuilder result = new StringBuilder();
		try {
			BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(file), "UTF-8"));// 构造一个BufferedReader类来读取文件
			String s = null;
			while ((s = br.readLine()) != null) {// 使用readLine方法，一次读一行
				result.append(System.lineSeparator() + s);
			}
			br.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return result.toString();
	}

	/**
	 * 获取文件的MD5
	 * 
	 * @param filePath
	 * @throws IOException
	 * @return String
	 */
	public static String getFileMD5(String filePath) throws IOException {
		File file = new File(filePath);
		String value = null;
		FileInputStream in = new FileInputStream(file);
		try {
			MappedByteBuffer byteBuffer = in.getChannel().map(FileChannel.MapMode.READ_ONLY, 0, file.length());
			MessageDigest md5 = MessageDigest.getInstance("MD5");
			md5.update(byteBuffer);
			BigInteger bi = new BigInteger(1, md5.digest());
			value = bi.toString(16);
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			if (null != in) {
				try {
					in.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
		return value;
	}

	/**
	 * 获取文件MD5码
	 * 
	 * @param filePath
	 * @param o
	 *            无用值
	 * @throws IOException
	 * @return String
	 */
	public static String getFileMD5(String filePath, Object o) throws IOException {
		File file = new File(filePath);
		FileInputStream in = new FileInputStream(file);
		String MD5 = DigestUtils.md5Hex(IOUtils.toByteArray(in));
		IOUtils.closeQuietly(in);
		return MD5;
	}

	/**
	 * 获取文件MD5码
	 * 
	 * @param MultipartFile
	 *            multipartfile
	 * @throws IOException
	 * @return String
	 */
	public static String getFileMD5(MultipartFile multipartfile) throws IOException {
		CommonsMultipartFile cf = (CommonsMultipartFile) multipartfile;
		DiskFileItem fi = (DiskFileItem) cf.getFileItem();
		File file = fi.getStoreLocation();
		FileInputStream in = new FileInputStream(file);
		String MD5 = DigestUtils.md5Hex(IOUtils.toByteArray(in));
		IOUtils.closeQuietly(in);
		return MD5;
	}

	/**
	 * 重新命名文件名称
	 * 
	 * @author Leo
	 * @param filePath
	 * @param name
	 * @return boolean
	 */
	public static boolean fileRename(String uploadPath, String fileName, String rename) {
		System.out.println("filePath:" + uploadPath);
		File file = new File(uploadPath + fileName); // 指定文件名及路径
		String filename = file.getAbsolutePath();
		if (filename.indexOf(".") >= 0) {
			filename = filename.substring(0, filename.lastIndexOf("."));
		}
		file.renameTo(new File(uploadPath + rename)); // 改名
		return true;
	}

//	public static void main(String[] args) {
//		File file = new File("E:/avatar.png"); // 指定文件名及路径
//		String name = "123";
//		String filename = file.getAbsolutePath();
//		if (filename.indexOf(".") >= 0) {
//			filename = filename.substring(0, filename.lastIndexOf("."));
//		}
//		file.renameTo(new File(name + ".png")); // 改名
//	
//	}
	
	/**
	 * 删除文件
	 * */
	public static void delFile(String filePath){
		File file = new File(filePath);
		if (file.exists()) {
		    file.delete();
		}
	}
}
